package com.thermal.thermal_inspection.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.RealName;
import com.thermal.thermal_inspection.entity.User;
import com.thermal.thermal_inspection.service.AccountService;
import com.thermal.thermal_inspection.vo.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.monitor.CounterMonitor;
import java.util.List;

@RestController
@RequestMapping("/users/account")
public class AccountController {
    private static final Logger logger = Logger.getLogger(AccountController.class);

    @Autowired
    AccountService accountService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public CommonResponse<LoginRespVO> login(@RequestParam String username, @RequestParam String password){
        System.out.println("进入登录接口！");
        LoginRespVO loginRespVO = accountService.login(username, password);
        if (loginRespVO != null)
            return CommonResponse.createForSuccess("SUCCESS", loginRespVO);
        return CommonResponse.createForError("登录失败");
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public CommonResponse<Object> register(@RequestParam String username, @RequestParam String password,
                                           @RequestParam String realName, @RequestParam String email,
                                           @RequestParam String phone, @RequestParam int role){
        int rows = accountService.register(username, password, realName, email, phone, role);
        if(rows == 0)
            return CommonResponse.createForError("注册失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 删除工人
     */
    @DeleteMapping("/deleteUser")
    public CommonResponse<Object> deleteUser(@RequestParam int userId){
        int rows = accountService.deleteUser(userId);
        if(rows == 0)
            return CommonResponse.createForError("删除工人失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     *查找所有工人
     */
    @GetMapping("/getUsers")
    public CommonResponse<List<GetUserRespVO>> getUsers(){
        List<GetUserRespVO> userList = accountService.getUsers();
        if(!userList.isEmpty())
            return CommonResponse.createForSuccess("SUCCESS", userList);
        return CommonResponse.createForError("查找工人失败");
    }

    /**
     * 修改信息
     */
    @PutMapping("/fixAccount")
    public CommonResponse<Object> fixAccount(@RequestParam int userId, @RequestParam String username, @RequestParam String realName,
                                             @RequestParam String email, @RequestParam String phone, @RequestParam int classId, @RequestParam int role){
        int rows = accountService.fixAccount(userId, username, realName, email, phone, classId, role);
        if(rows == 0)
            return CommonResponse.createForError("修改信息失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 查找个人信息
     */
    @GetMapping("/getUserInfo")
    public CommonResponse<UserInfoVO> getUserInfo(int userId){
        UserInfoVO userInfo = accountService.getUserInfo(userId);
        if(userInfo == null)
            return CommonResponse.createForError("查找个人信息失败");
        return CommonResponse.createForSuccess("SUCCESS", userInfo);
    }

    /**
     * 修改密码
     */
    @PutMapping("/fixPassword")
    public CommonResponse<Object> fixPassword(@RequestParam("userId") int requestUserId,
                                              @RequestBody PasswordVO passwordVO){
        int userId = passwordVO.getUserId();
        String oldPassword = passwordVO.getOldPassword();
        String newPassword = passwordVO.getNewPassword();
        if(!accountService.existUser(userId))
            return CommonResponse.createForError("用户不存在");
        //先验证身份，是工人还是管理员，管理员则无需验证身份，工人需要判断密码是否相同
        int rows1 = accountService.isOldPassword(requestUserId, userId, oldPassword);
        if(rows1 == 0)
            return CommonResponse.createForError("原密码输入错误");
        int rows2 = accountService.fixPassword(userId, newPassword);
        if(rows2 == 0)
            return CommonResponse.createForError("修改密码失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 查询班组下所有用户
     */
    @GetMapping("/selectByClass")
    public CommonResponse<List<ClassWorkerVO>> selectByClass(){
        List<ClassWorkerVO> classWorkerVOList= accountService.selectByClass();
        if(classWorkerVOList.isEmpty()){
            logger.error("未查询到班组成员信息");
            return CommonResponse.createForError("未查询到班组成员信息");
        }
        logger.info("查询班组成员信息成功");
        return CommonResponse.createForSuccess("SUCCESS", classWorkerVOList);
    }

    /**
     * 按照工人真实姓名查询工人
     */
    @GetMapping("/getUser/{userName}")
    public CommonResponse<List<RealName>> getUserByRealName(@PathVariable String userName){
        List<RealName> realNameList = accountService.getUserByRealName(userName);
        if(realNameList.isEmpty() || realNameList == null){
            logger.error("未查询到工人信息");
            return CommonResponse.createForError("未查询到工人信息");
        }
        logger.info("查询成功");
        return CommonResponse.createForSuccess("SUCCESS", realNameList);
    }
}
